package com.fub.update;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;

import com.fub.guidance.BuildingVersionDAO;
import com.fub.util.PreferenceDAO;

public class SDCardReader {

	private ServerStreamParser netManager;
	private BuildingVersionDAO buildingVersionDBService;
	private PreferenceDAO preferenceDAO;
	private Dialog prozessDialog;
	private String buildingId;
	private String filename;
	private Context context;
	private Handler handler;

	/**
	 * Constructs a new sd-card reader
	 * 
	 * @param context
	 *            the application context
	 */
	public SDCardReader(Context context) {
		this.context = context;
		this.netManager = new ServerStreamParser(context);
		this.buildingVersionDBService = new BuildingVersionDAO(context);
		this.preferenceDAO = new PreferenceDAO(context);
		this.handler = new Handler();
	}

	/**
	 * Returns a list of xml files at the root directory of the sd-card.
	 * 
	 * @return a list of xml files at the root directory of the sd-card
	 */
	public List<String> getFubFiles() {
		List<String> result = new ArrayList<String>();
		File currentDir = new File("/sdcard/");
		File[] dirsFiles = currentDir.listFiles();
		for (File f : dirsFiles) {
			if (f.isFile() && f.getName().contains("xml")) {
				result.add(f.getName());
			}
		}
		return result;
	}

	/**
	 * Starts the import of an new building.
	 * 
	 * @param buildingId
	 *            the id of the building
	 * @param filename
	 *            the name of the xml file that contains the building data
	 */
	public void startAddNewBuildingThread(String buildingId, String filename) {
		this.buildingId = buildingId;
		this.filename = filename;
		showProcessDialog();
		Thread thread = new Thread() {
			@Override
			public void run() {
				addNewBuilding();
				SDCardReader.this.handler
						.post(SDCardReader.this.hideProcessDialog);
			}
		};
		thread.start();
	}

	private void addNewBuilding() {
		File f = new File(Environment.getExternalStorageDirectory()
				+ File.separator + this.filename);
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		this.netManager.parseNodesEdgesWallsRelations(inputStream);
		addBuildingToVersionTable(this.buildingId, 0, 0);
		this.buildingVersionDBService.updateBuildingVersion(this.buildingId, 0);
		this.preferenceDAO.setBuildingId(this.buildingId);
	}

	private void addBuildingToVersionTable(String buildingId, float latitude,
			float longitude) {
		if (this.buildingVersionDBService
				.isBuildingInVersionControll(buildingId)) {
			this.buildingVersionDBService
					.resetBuildingInVersionControll(buildingId);
		} else {
			this.buildingVersionDBService.insertBuildingToVersionTable(
					buildingId, latitude, longitude);
		}
	}

	private Runnable hideProcessDialog = new Runnable() {
		@Override
		public void run() {
			SDCardReader.this.prozessDialog.hide();
		}
	};

	private void showProcessDialog() {
		this.prozessDialog = ProgressDialog
				.show(this.context,
						"",
						this.context.getResources().getString(
								com.fub.R.string.importRunning), true);
		this.prozessDialog.show();
	}
}
